package com.hua.api.modules.order.dao;


import com.hua.api.core.constants.Status;
import com.hua.api.core.framework.statement.EntityManagerTemplate;
import com.hua.api.core.framework.statement.Statement;
import com.hua.api.core.framework.statement.mysql.StatementFactoryImpl;
import com.hua.api.modules.order.domain.OrderBid;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class OrderBidDao extends EntityManagerTemplate {



    public List<OrderBid> queryOrderBidUseOrderId(Integer orderId){
        Statement stms = StatementFactoryImpl.getInstance().createStatement(" from OrderBid o where o.orderId=?1 and o.state!=?2");
        stms.addParams(orderId, Status.BidStatus.yiquxiao.getValue());
        return query(stms,OrderBid.class);

    }


    public List<Map<String,Object>> queryShopInfoWithDistance(Double longitude, Double latitude, Integer distance, Integer shopId,List<Integer> shopIds){
        Statement stms = StatementFactoryImpl.getInstance().createNativeStatement("select round(st_distance(point(s.`longitude`,s.`latitude`),point(?1,?2))*111195) as distance,s.*  from `shop_info` s where s.id!=?3 having distance<?4");
        // Statement stms = StatementFactoryImpl.getInstance().createNativeStatement("select round(st_distance(point(s.`longitude`,s.`latitude`),point(?1,?2))*111195) as distance,s.id,s.user_id ,u.phone_number ");
        // stms.append(" from `shop_info` s ,user_info u where s.user_id = u.id and s.id!=?3 having distance<?4");
        stms.addParams(longitude,latitude,shopId,distance);

        stms.appendIn("and","s.id",shopIds);

        stms.addOrderBy("  distance asc");
        return  queryListMap(stms);
    }



    public List<OrderBid> queryOrderOnBidFinish(Integer orderId){
        Statement stms = StatementFactoryImpl.getInstance().createStatement(" from OrderBid o where o.orderId=?1 and o.state=?2");
        stms.addParams(orderId, Status.BidStatus.dealing.getValue());
        return query(stms,OrderBid.class);
    }

    public List<OrderBid>  querNeedFinishOrderBid(Date beginDate, Date endDate){
        Statement stms = StatementFactoryImpl.getInstance().createStatement(" select ob from OrderBid ob,OrderInfo o where ob.orderId=o.id and  ob.createdTime>=?1 and ob.createdTime<?2 and ob.state=?3 and o.status=?4");
                  stms.addParams(beginDate, endDate,Status.BidStatus.dealing.getValue(),Status.OrderStatus.wancheng.getValue());
        return query(stms,20,OrderBid.class);
    }


}
